"use strict";

// signup controller
app.controller("ForgotpwdController", ["$scope", "$http", "$translate", "AuthService",
                               function($scope,   $http,   $translate,   AuthService) {
    $scope.user = {
    		email: "",
    		securityCode: "",
    		password: null,
    		verifyKey: "",
    		verifyCode: "",
    		verifyUrl: ""
    };
    $scope.authError = null;
    $scope.isCollapsed = true;
    	
    $scope.sendEmail = function() {
    	var accessUrl = AuthService.getRsContext() + "/rest/verifycode/verify?verifyKey=" 
    		+ $scope.user.verifyKey + "&verifyCode=" + $scope.user.verifyCode;
		$http({
	        method: 'GET',
	        url: accessUrl
	    }).then(function successCallback(response) {
	    	console.log("/rest/verifycode/verify  response ==========", response);
	        var code = response.data.code;
	    	if(code != 0) {
	    		$scope.authError = $scope.selectLang=="English" ? response.data.enMessage : response.data.message;
	    		initVerifyCode();
	    		return;
	    	}
	    	if(code == 0) {
	    		// send mail
	    		$http.post(AuthService.getRsContext() + "/rest/user/createSecurityCodeByEmail?email=" + $scope.user.email)
	    			.then(
	    			function(response) {
	    				console.log("==============response --- response=", response);
	    				var code = response.data.code;
	    				if (code == 0) {
	    					$scope.isCollapsed = !$scope.isCollapsed;
	    					$scope.authError = $translate.instant("common.sendCompleted");
	    				} else {
	    					$scope.authError = ($scope.selectLang == "English" ? response.data.enMessage : response.data.message);
	    				}
	    			},
	    			function(x) {
	    				$scope.authError = "Server Error";
	    			});
	    	}
	    }, function errorCallback(response) {
	    	$scope.authError = $translate.instant("rest.error.exception");
	    	initVerifyCode();
	    	return;
	    });
    	
    };
    
    $scope.updatePwd = function() {
    	var param = "email=" + $scope.user.email + "&securityCode=" + $scope.user.securityCode +"&newPassword=" + $scope.user.password;
    	$http.post(AuthService.getRsContext() + "/rest/user/resetPasswordByEmail?" + param)
		.then(
			function(response) {
				var code = response.data.code;
				if (code == 0) {
					$scope.user = {};
					$scope.authError = $translate.instant("user.passwordSuccess");
				} else {
					$scope.authError = $scope.selectLang == "English" ? 
							response.data.enMessage : response.data.message;
				}
			},
			function(x) {
				$scope.authError = "Server Error";
			});
    };

    var codeLength = 4;
	var random = new Array(1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
    $scope.refreshVerifyCode = function() {
    	initVerifyCode();
    };
    var initVerifyCode = function() {
    	var codeKey = "";
    	for(var i=0; i<codeLength; i++) {
    		var index = Math.floor(Math.random()*34);
    		codeKey += random[index];
    	}
    	$scope.user.verifyCode = "";
    	codeKey = codeKey + (new Date()).getTime();
    	$scope.user.verifyKey = codeKey
    	$scope.user.verifyUrl = AuthService.getRsContext() + "/rest/verifycode/image?verifyKey=" + codeKey;
    };
    initVerifyCode();

 }]);
